module VinaRoutine

using Vina
using Utils

function vina_functions(config::Config, configfilename::String)
    println(h("\n                    ************ Vina functions ************", fc="c"))
    println(" 0 Return")
    println(" 1 Prepare config.txt file for audodock vina")
    println(" 2 Prepare config.txt file for LeDock")
    input = parse(Int32, readline())
    if input == 0
        return
    elseif input == 1
        prepare_vina_config!(config, configfilename)
    end
end

function run_vina_dock(config_file)
    run(Cmd(`vina --config $(basename(config_file))`, dir=dirname(config_file)))
end

function prepare_vina_config!(config::Config, configfilename::String)
    while true
        println(h("\n                    ************ prepare_vina_config ************", fc="c"))
        println("-10 Return")
        println("-2 All miscellaneous parameters (4-8) reset to default")
        println("-1 Show current docking parameters")
        println(" 0 Generate docking config file!")
        println(" 1 Edit docking ligand name, Current: " * config.ligand)
        println(" 2 Edit the generated docking structures file name, Current: " * config.output)
        println(" 3 Edit the output log file name, Current: " * config.log * ".txt")
        println(" 4 Edit cpu number, Current: " * (config.cpu_num == 0 ? "Default" : "$(config.cpu_num)"))
        println(" 5 Edit seed, Current: " * (config.seed == 0 ? "Default" : "$(config.seed)"))
        println(" 6 Edit exhaustiveness, Current: " * "$(config.exhaustiveness)")
        println(" 7 Edit num_modes, Current: " * "$(config.num_modes)")
        println(" 8 Edit energy_range, Current: " * "$(config.energy_range)")
        para = sel_fun(Int32)
        if para == 0    # 获取输出配置文件名
            out_path = get_path(configfilename)
            gen_vina_config(config, out_path)
            finish_path(out_path)
            request_hint("Automatically run vina now? [Y/n]")
            if get_input("y") != "n"
                run_vina_dock(out_path)
            end
            finish_hint("Finished docking at $(dirname(out_path)).\n")
            return
        else
            if para == -10
                return
            elseif para == -2
                config.cpu_num = 0
                config.seed = 0
                config.exhaustiveness = 8
                config.num_modes = 9
                config.energy_range = 3.0
            elseif para == -1
                Vina.print_config(config)
            elseif para == 1    # 获取配体文件名
                println("\nPlease input the ligand file name, e.g., ligand.pdbqt")
                println("If not specified, the ligand file name will be set as \"ligand.pdbqt\"")
                ligandname = readline()
                if length(strip(ligandname)) != 0
                    if endswith(ligandname, ".pdbqt")
                        ligandname = ligandname[1:end-6]
                    end
                    config.ligand = ligandname
                    config.output = "$(ligandname)_out"
                else
                    config.ligand = "ligand"
                end
            elseif para == 2    # 获取输出对接结构文件名
                println("\nPlease input the output docking structure file name, e.g., E:/Data/ligand_out.pdbqt")
                println("If not specified, the output docking structure file name will be set as \"ligand_out.pdbqt\"")
                output_docking_file_name = readline()
                if length(strip(output_docking_file_name)) != 0
                    if endswith(output_docking_file_name, ".pdbqt")
                        output_docking_file_name = output_docking_file_name[1:end-6]
                    end
                    config.output = output_docking_file_name
                else
                    config.output = "ligand_out"
                end
            elseif para == 3    # 获取输出日志文件名
                println("\nPlease input the log file name, e.g., E:/Data/log.txt")
                println("If not specified, the log file name will be set as \"log.txt\"")
                log_file_name = readline()
                if length(strip(log_file_name)) != 0
                    if endswith(log_file_name, ".txt")
                        log_file_name = log_file_name[1:end-4]
                    end
                    config.log = log_file_name
                else
                    config.log = "log"
                end
            elseif para == 4    # cpu num
                println("\nPlease input the number of CPUs to use, e.g., 8")
                println("(the default is to try to detect the number of CPUs or, failing that, use 1)")
                cpu_num = readline()
                if length(strip(cpu_num)) != 0
                    cpu_num = parse(Int32, cpu_num)
                    config.cpu_num = cpu_num
                else
                    config.cpu_num = 0
                end
            elseif para == 5    # seed
                println("\nPlease input the explicit random seed, e.g., 42")
                seed = readline()
                if length(strip(seed)) != 0
                    seed = parse(Int32, seed)
                    config.seed = seed
                else
                    config.seed = 0
                end
            elseif para == 6    # exhaustiveness
                println("\nPlease input the exhaustiveness of the global search (roughly proportional to time): 1+, e.g., 8")
                exhaustiveness = readline()
                if length(strip(exhaustiveness)) != 0
                    exhaustiveness = parse(Int32, exhaustiveness)
                    config.exhaustiveness = exhaustiveness
                else
                    config.exhaustiveness = 8
                end
            elseif para == 7    # number of modes
                println("\nPlease input the maximum number of binding modes to generate, e.g., 9")
                num_modes = readline()
                if length(strip(num_modes)) != 0
                    num_modes = parse(Int32, num_modes)
                    config.num_modes = num_modes
                else
                    config.num_modes = 9
                end
            elseif para == 8    # energy range
                println("\nPlease input the maximum energy difference between the best binding mode and the worst one displayed (kcal/mol), e.g., 3")
                energy_range = readline()
                if length(strip(energy_range)) != 0
                    energy_range = parse(Float64, energy_range)
                    config.energy_range = energy_range
                else
                    config.energy_range = 3.0
                end
            end
        end
    end
end

end